import { getToken, setToken } from '@/utils/auth'
import { getUserInfo, logUserOut } from '@/api/user'

const user = {
    namespaced: true,
    state: {
        username: undefined,
        token: getToken(),
        avatar: 'https://img1.baidu.com/it/u=3418981113,1954223259&fm=26&fmt=auto'
    },
    mutations: {
        SET_USERINFO: (state, userInfo) => {
            state.username = userInfo.username
        },
        SET_TOKEN: (state, token) => {
            state.token = token
        }
    },
    actions: {
        async GetUserInfo ({ commit }) {
            const { data } = await getUserInfo()
            console.log('提交了一下', data)
            commit('SET_USERINFO', data)
        },
        // 用户名登录
        // Login ({ commit }, token) {
        //     console.log('提交了一下-login')
        //     return new Promise((resolve, reject) => {
        //         setToken(token)
        //         commit('SET_TOKEN', token)
        //         resolve()
        //     })
        // },
        async FedLogOut ({ commit }) {
            commit('SET_TOKEN', '')
            return await logUserOut()
        }
    }
}

export default user
